package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhijian.medical.dao.local.SopRapidPathologicalMapper;
import com.zhijian.medical.entity.po.SopRapidPathological;
import com.zhijian.medical.entity.vo.request.SopRapidPathologicalSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.SopRapidPathologicalSaveVo;
import com.zhijian.medical.entity.vo.request.SopRapidPathologicalSearchVo;
import com.zhijian.medical.entity.vo.response.SopRapidPathologicalVo;
import com.zhijian.medical.service.SopRapidPathologicalService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * SOP-术中快速病理考核业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-11-20 11:27:37
 */
@Slf4j
@Service
public class SopRapidPathologicalServiceImpl implements SopRapidPathologicalService {

	@Autowired
    private SopRapidPathologicalMapper mapper;

    @Override
    public SopRapidPathological selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public SopRapidPathologicalVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(SopRapidPathological sopRapidPathological) {
        Date now = new Date();
        sopRapidPathological.setCreateTime(now);
        sopRapidPathological.setUpdateTime(now);
        return mapper.insert(sopRapidPathological) > 0 ? sopRapidPathological.getId() : 0L;
    }

    @Override
    public Long insertSelective(SopRapidPathological sopRapidPathological) {
        Date now = new Date();
        sopRapidPathological.setCreateTime(now);
        sopRapidPathological.setUpdateTime(now);
        return mapper.insertSelective(sopRapidPathological) > 0 ? sopRapidPathological.getId() : 0L;
    }

    @Override
    public boolean update(SopRapidPathological sopRapidPathological) {
        Date now = new Date();
        sopRapidPathological.setUpdateTime(now);
        return mapper.updateByPrimaryKey(sopRapidPathological) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(SopRapidPathological record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(SopRapidPathologicalSaveOrUpdateVo saveOrUpdateVo) {
        SopRapidPathological sopRapidPathological = new SopRapidPathological();
        BeanUtils.copyProperties(saveOrUpdateVo, sopRapidPathological);
        return this.insert(sopRapidPathological) != 0;
    }

    @Override
    public boolean saveList(SopRapidPathologicalSaveVo param) {
        JSONArray jsonArray = new JSONArray();
        for (SopRapidPathologicalSaveOrUpdateVo record : param.getRecordList()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("pathologicalNum", record.getPathologicalNum());
            jsonObject.put("referringCompare", record.getReferringCompare());
            jsonObject.put("mark", record.getMark());
            jsonObject.put("rapidCompare", record.getRapidCompare());
            jsonArray.add(jsonObject);
        }
        SopRapidPathologicalSaveOrUpdateVo saveOrUpdateVo = new SopRapidPathologicalSaveOrUpdateVo();
        BeanUtils.copyProperties(param, saveOrUpdateVo);
        saveOrUpdateVo.setDataJson(jsonArray.toString());
        return this.save(saveOrUpdateVo);
    }

    @Override
    public boolean modifyNew(SopRapidPathologicalSaveVo param) {
        JSONArray jsonArray = new JSONArray();
        for (SopRapidPathologicalSaveOrUpdateVo record : param.getRecordList()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("pathologicalNum", record.getPathologicalNum());
            jsonObject.put("referringCompare", record.getReferringCompare());
            jsonObject.put("mark", record.getMark());
            jsonObject.put("rapidCompare", record.getRapidCompare());
            jsonArray.add(jsonObject);
        }
        SopRapidPathologicalSaveOrUpdateVo saveOrUpdateVo = new SopRapidPathologicalSaveOrUpdateVo();
        BeanUtils.copyProperties(param, saveOrUpdateVo);
        saveOrUpdateVo.setDataJson(jsonArray.toString());
        return this.modify(saveOrUpdateVo);
    }

    @Override
    public boolean modify(SopRapidPathologicalSaveOrUpdateVo saveOrUpdateVo) {
        SopRapidPathological old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        SopRapidPathological sopRapidPathological = new SopRapidPathological();
        BeanUtils.copyProperties(saveOrUpdateVo, sopRapidPathological);
        return this.update(sopRapidPathological);
    }

    @Override
    public SopRapidPathologicalVo selectOne(SopRapidPathologicalSearchVo sopRapidPathologicalSearchVo) {
        return mapper.selectOne(sopRapidPathologicalSearchVo);
     }

    @Override
    public Integer count(SopRapidPathologicalSearchVo sopRapidPathologicalSearchVo) {
        return mapper.count(sopRapidPathologicalSearchVo);
    }

    @Override
    public List<SopRapidPathologicalVo> selectList(SopRapidPathologicalSearchVo sopRapidPathologicalSearchVo) {
        return mapper.selectList(sopRapidPathologicalSearchVo);
    }

    @Override
    public Page<SopRapidPathologicalVo> selectPage(SopRapidPathologicalSearchVo sopRapidPathologicalSearchVo) {
        AssertUtil.check(Objects.isNull(sopRapidPathologicalSearchVo.getPageNum()) || Objects.isNull(sopRapidPathologicalSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(sopRapidPathologicalSearchVo);
        //分页
        Page<SopRapidPathologicalVo> page = new Page<>(true, count, sopRapidPathologicalSearchVo.getPageNum(), sopRapidPathologicalSearchVo.getPageSize());
        sopRapidPathologicalSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<SopRapidPathologicalVo> responseVos = this.selectList(sopRapidPathologicalSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<SopRapidPathological> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(SopRapidPathologicalSearchVo sopRapidPathologicalSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(sopRapidPathologicalSearchVo);
    }
    
}